﻿using System.Web.Mvc;
using MVCDemo.Models;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Data.EntityModel;
using System.Collections;
using System.Collections.Generic;
using System;
using MVCDemo.Common;

namespace MVCDemo.Controllers
{
    public class EmployeeExController : Controller
    {
        //
        // GET: /EmployeeEx/
        [OutputCache(CacheProfile="1MinuteCache")]
        public ActionResult Index()
        {
            EmployeeExContext db = new EmployeeExContext();
            return View(db.EmployeeExes.ToList());
        }

        [ChildActionOnly]
        [PartialCache("1MinuteCache")]
        public string GetEmployeeCount()
        {
            EmployeeExContext db = new EmployeeExContext();
            return "Employee count = " + db.EmployeeExes.Count().ToString() + " @" + DateTime.Now.ToString();
        }

        //
        // GET: /EmployeeEx/Details/5

        public ActionResult Details(int id)
        {
            EmployeeExContext db = new EmployeeExContext();
            EmployeeEx employee = db.EmployeeExes.Single(x => x.Id == id);
            return View(employee);
        }

        public ActionResult CompanyDirector()
        {
            Company company = new Company();
            return View(company);
        }

        [HttpPost]
        public ActionResult Delete(IEnumerable<int> employeeIdsToDelete)
        {
            EmployeeExContext db = new EmployeeExContext();
            db.EmployeeExes.Where(x => employeeIdsToDelete.Contains(x.Id)).ToList().ForEach(x => db.EmployeeExes.Remove(x));
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /EmployeeEx/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /EmployeeEx/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /EmployeeEx/Edit/5

        public ActionResult Edit(int id)
        {
            EmployeeExContext db = new EmployeeExContext();
            EmployeeEx employee = db.EmployeeExes.Single(x => x.Id == id);
            return View(employee);
        }

        //
        // POST: /EmployeeEx/Edit/5

        [HttpPost]
        public ActionResult Edit(EmployeeEx employee)
        {
            if (ModelState.IsValid)
            {
                EmployeeExContext db = new EmployeeExContext();
                EmployeeEx employeeFromDb = db.EmployeeExes.Single(x => x.Id == employee.Id);

                employeeFromDb.FullName = employee.FullName;
                employeeFromDb.Gender = employee.Gender;
                employeeFromDb.Age = employee.Age;
                employeeFromDb.HireDate = employee.HireDate;
                employeeFromDb.Salary = employee.Salary;
                employeeFromDb.PersonalWebSite = employee.PersonalWebSite;
                db.SaveChanges();

                return RedirectToAction("Details", new { id = employee.Id });
            }
            return View(employee);
        }

        ////
        //// GET: /EmployeeEx/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /EmployeeEx/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
    }
}
